import time
#
import Py_Vissim.FunctionC as FC
from Py_Vissim.Crossing import Crossing2 as C2
from Py_Vissim.Crossing import Crossing4 as C4
import win32com.client as com
import Py_Vissim.Update_database as Update_database
import lib.db as database

# import FunctionC as FC
# from Crossing import Crossing2 as C2
# from Crossing import Crossing4 as C4
# import win32com.client as com
# import Update_database as Update_database


def StartSimulation_Click(vissim_road):
    # get all the road name by their id first
    db = database.database()
    Road_name = {}
    for item in db.Read_Road():
        Road_name[item[0]] = item[1]

    db.Delete_His_Bus()

    # ======================================== #

    Vissim = com.Dispatch("Vissim.Vissim")
    # Vissim.LoadNet("C:\\Users\\LENOVO\Desktop\\tramsfrom\\Py_Vissim\\road\\VissimRoad.inp")
    Vissim.LoadNet("C:\\Users\\LENOVO\\Desktop\\UI_Project\\Py_Vissim\\road_v2.0\\road.inp")
    Vissim.LoadNet(vissim_road)
    Vissim.Simulation.Speed = 1  # 仿真速度
    Vissim.Simulation.Period = 3000  # 仿真周期

    # <editor-fold desc="路口1">
    signal1 = Vissim.Net.SignalControllers.GetSignalControllerByNumber(1)
    signal1_group1 = signal1.SignalGroups.GetSignalGroupByNumber(1)
    signal1_group2 = signal1.SignalGroups.GetSignalGroupByNumber(2)
    signal1_group3 = signal1.SignalGroups.GetSignalGroupByNumber(3)
    signal1_group4 = signal1.SignalGroups.GetSignalGroupByNumber(4)
    signal1_group1.SetAttValue("TYPE", 2)  # 1变绿
    signal1_group2.SetAttValue("TYPE", 3)  # 2变红
    signal1_group3.SetAttValue("TYPE", 3)  # 3变红
    signal1_group4.SetAttValue("TYPE", 3)  # 4变红
    signal1_detector1 = signal1.Detectors.GetDetectorByNumber(1)
    signal1_detector2 = signal1.Detectors.GetDetectorByNumber(2)
    lukou1 = C4(lukou_ID=1, t_g_1=20, t_g_2=10, t_g_3=20, t_g_4=10, delta_t_g_max=10, delta_t_r_max=10)
    lukou1.Set4()
    lukou1_data1 = Vissim.Net.DataCollections.GetDataCollectionByNumber(9)
    lukou1_data2 = Vissim.Net.DataCollections.GetDataCollectionByNumber(12)
    lukou1_data3 = Vissim.Net.DataCollections.GetDataCollectionByNumber(8)
    lukou1_data4 = Vissim.Net.DataCollections.GetDataCollectionByNumber(16)

    # </editor-fold>

    # <editor-fold desc="路口2">
    signal2 = Vissim.Net.SignalControllers.GetSignalControllerByNumber(2)
    signal2_group1 = signal2.SignalGroups.GetSignalGroupByNumber(1)
    signal2_group2 = signal2.SignalGroups.GetSignalGroupByNumber(2)
    signal2_group3 = signal2.SignalGroups.GetSignalGroupByNumber(3)
    signal2_group4 = signal2.SignalGroups.GetSignalGroupByNumber(4)
    signal2_group1.SetAttValue("TYPE", 2)  # 1变绿
    signal2_group2.SetAttValue("TYPE", 3)  # 2变红
    signal2_group3.SetAttValue("TYPE", 3)  # 3变红
    signal2_group4.SetAttValue("TYPE", 3)  # 4变红
    signal2_detector1 = signal2.Detectors.GetDetectorByNumber(1)
    signal2_detector2 = signal2.Detectors.GetDetectorByNumber(2)
    lukou2 = C4(lukou_ID=2, t_g_1=20, t_g_2=10, t_g_3=20, t_g_4=10, delta_t_g_max=10, delta_t_r_max=10)
    lukou2.Set4()

    lukou2_data1 = Vissim.Net.DataCollections.GetDataCollectionByNumber(20)
    lukou2_data2 = Vissim.Net.DataCollections.GetDataCollectionByNumber(23)
    lukou2_data3 = Vissim.Net.DataCollections.GetDataCollectionByNumber(26)
    lukou2_data4 = Vissim.Net.DataCollections.GetDataCollectionByNumber(30)
    # </editor-fold>

    # <editor-fold desc="路口3">
    signal3 = Vissim.Net.SignalControllers.GetSignalControllerByNumber(3)
    signal3_group1 = signal3.SignalGroups.GetSignalGroupByNumber(1)
    signal3_group2 = signal3.SignalGroups.GetSignalGroupByNumber(2)
    signal3_group3 = signal3.SignalGroups.GetSignalGroupByNumber(3)
    signal3_group4 = signal3.SignalGroups.GetSignalGroupByNumber(4)
    signal3_group1.SetAttValue("TYPE", 2)  # 1变绿
    signal3_group2.SetAttValue("TYPE", 3)  # 2变红
    signal3_group3.SetAttValue("TYPE", 3)  # 3变红
    signal3_group4.SetAttValue("TYPE", 3)  # 4变红
    signal3_detector1 = signal3.Detectors.GetDetectorByNumber(1)
    signal3_detector2 = signal3.Detectors.GetDetectorByNumber(2)
    lukou3 = C4(lukou_ID=3, t_g_1=20, t_g_2=10, t_g_3=20, t_g_4=10, delta_t_g_max=10, delta_t_r_max=10)
    lukou3.Set4()

    lukou3_data1 = Vissim.Net.DataCollections.GetDataCollectionByNumber(34)
    lukou3_data2 = Vissim.Net.DataCollections.GetDataCollectionByNumber(37)
    lukou3_data3 = Vissim.Net.DataCollections.GetDataCollectionByNumber(39)
    lukou3_data4 = Vissim.Net.DataCollections.GetDataCollectionByNumber(41)
    # </editor-fold>

    # <editor-fold desc="路口4">
    signal4 = Vissim.Net.SignalControllers.GetSignalControllerByNumber(4)
    signal4_group1 = signal4.SignalGroups.GetSignalGroupByNumber(1)
    signal4_group2 = signal4.SignalGroups.GetSignalGroupByNumber(2)
    signal4_group1.SetAttValue("TYPE", 2)  # 1变绿
    signal4_group2.SetAttValue("TYPE", 3)  # 2变红
    signal4_detector1 = signal4.Detectors.GetDetectorByNumber(1)
    signal4_detector2 = signal4.Detectors.GetDetectorByNumber(2)
    lukou4 = C2(lukou_ID=4, t_green=30, t_red=30, delta_t_g_max=10, delta_t_r_max=10, M=1)
    lukou4.Set2()
    # print('lukou4:', lukou4.delta_t)

    '''new_2'''
    lukou4_data1 = Vissim.Net.DataCollections.GetDataCollectionByNumber(1)
    lukou4_data2 = Vissim.Net.DataCollections.GetDataCollectionByNumber(1)
    lukou4_data3 = Vissim.Net.DataCollections.GetDataCollectionByNumber(5)

    '''end new_2'''
    # </editor-fold>

    # <editor-fold desc="路口5">
    signal5 = Vissim.Net.SignalControllers.GetSignalControllerByNumber(5)
    signal5_group1 = signal5.SignalGroups.GetSignalGroupByNumber(1)
    signal5_group2 = signal5.SignalGroups.GetSignalGroupByNumber(2)
    signal5_group3 = signal5.SignalGroups.GetSignalGroupByNumber(3)
    signal5_group4 = signal5.SignalGroups.GetSignalGroupByNumber(4)
    signal5_group1.SetAttValue("TYPE", 2)  # 1变绿
    signal5_group2.SetAttValue("TYPE", 3)  # 2变红
    signal5_group3.SetAttValue("TYPE", 3)  # 3变红
    signal5_group4.SetAttValue("TYPE", 3)  # 4变红
    signal5_detector1 = signal5.Detectors.GetDetectorByNumber(1)
    signal5_detector2 = signal5.Detectors.GetDetectorByNumber(2)
    lukou5 = C4(lukou_ID=5, t_g_1=20, t_g_2=10, t_g_3=19, t_g_4=1, delta_t_g_max=10, delta_t_r_max=10)
    lukou5.Set4()

    lukou5_data1 = Vissim.Net.DataCollections.GetDataCollectionByNumber(43)
    lukou5_data2 = Vissim.Net.DataCollections.GetDataCollectionByNumber(45)
    lukou5_data3 = Vissim.Net.DataCollections.GetDataCollectionByNumber(48)
    lukou5_data4 = Vissim.Net.DataCollections.GetDataCollectionByNumber(50)
    # </editor-fold>

    for i in range(int(Vissim.Simulation.Period)):
        Vissim.Simulation.RunSingleStep()

        if i % 5 == 0:
            Update_database.Update_Vis_db(Vissim, i, Road_name)
            # t = threading.Thread(target=Update_database.Update_Vis_db, args=(Vissim,))
            # t.setDaemon(True)
            # t.start()

        FC.Nextstep4(lukou1, signal1_group1, signal1_group2, signal1_group3, signal1_group4, lukou1_data1, lukou1_data2,
                     lukou1_data3, lukou1_data4)
        FC.Nextstep4(lukou2, signal2_group1, signal2_group2, signal2_group3, signal2_group4, lukou2_data1, lukou2_data2,
                     lukou2_data3, lukou2_data4)
        FC.Nextstep4(lukou3, signal3_group1, signal3_group2, signal3_group3, signal3_group4, lukou3_data1, lukou3_data2,
                     lukou3_data3, lukou3_data4)
        FC.Nextstep4(lukou5, signal5_group1, signal5_group2, signal5_group3, signal5_group4, lukou5_data1, lukou5_data2,
                     lukou5_data3, lukou5_data4)
        FC.Getbus4(i, Vissim, lukou1, signal1_detector1, signal1_detector2)
        FC.Getbus4(i, Vissim, lukou2, signal2_detector1, signal2_detector2)
        FC.Getbus4(i, Vissim, lukou3, signal3_detector1, signal3_detector2)
        FC.Getbus4(i, Vissim, lukou5, signal5_detector1, signal5_detector2)
        if i > 40:
            FC.Nextstep2(lukou4, signal4_group1, signal4_group2, lukou4_data1, lukou4_data2, lukou4_data3)
            FC.Getbus2(Vissim, lukou4, signal4_detector1, signal4_detector2)
            # try:
            #     FC.Nextstep2(lukou4, signal4_group1, signal4_group2, lukou4_data1, lukou4_data2, lukou4_data3)
            #     FC.Getbus2(Vissim, lukou4, signal4_detector1, signal4_detector2)
            # except:
            #     print("i=", i, end=" ")
            #     print("error")
            #     pass

        time.sleep(0.1)
    Vissim.Simulation.stop()
